package com.middlegroup.subjectpay.service.impl;

import com.middlegroup.common.enums.ServiceType;
import com.middlegroup.common.exception.ErrorMsg;
import com.middlegroup.common.model.TransResult;
import com.middlegroup.subjectpay.dto.TransRequest;
import com.middlegroup.subjectpay.entity.CardBin;
import com.middlegroup.subjectpay.entity.SubjectOutBills;
import com.middlegroup.subjectpay.service.*;
import com.middlegroup.subjectpay.service.empty.EmptyService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * Created by pc on 2019/1/14.
 */
@Service
@Slf4j
public class DfServiceImpl implements DfService {
    @Resource
    private CardBinService cardBinService;
    @Resource
    private SubjectOutBillsService subjectOutBillsService;
    @Resource
    private ChangpayService changpayService;
    @Resource
    private HelipayService helipayService;
    @Resource
    private KjtpayService kjtpayService;

    @Resource
    private EmptyService emptyService;

    @Override
    public TransResult df(TransRequest request) {
        if (StringUtils.isBlank(request.getServiceType())){
            request.setServiceType(ServiceType.DF.name());
        }

        TransResult transResult = new TransResult();
        transResult.setBillNo(request.getBillNo());
        transResult.setMerchantBillNo(request.getMerchantBillNo());
        transResult.setAmount(request.getAmount());
        transResult.setTransStatus(0);
        transResult.setTransTime(System.currentTimeMillis());

        SubjectOutBills subjectOutBills=new SubjectOutBills();
        subjectOutBills.setSubjectCode(request.getSubjectCode());
        subjectOutBills.setMerchantBillNo(request.getMerchantBillNo());
        subjectOutBills.setBillNo(request.getBillNo());
        subjectOutBills.setChannelCode(request.getChannelCode().getCode());
        subjectOutBills.setServiceType(request.getServiceType());
        subjectOutBills.setAmount(request.getAmount());
        subjectOutBills.setStatus(0);

        boolean rt = false;
        try {
            rt = subjectOutBillsService.insert(subjectOutBills);
        }catch (Exception e){
            log.error("插入主体端出款订单表(t_subject_out_bills)失败:"+request.getBillNo(),e);
        }

        if (!rt) {
            //插入失败
            transResult.setTransStatus(2);
            transResult.setMsg("插入主体端出款订单表(t_subject_out_bills)失败:"+request.getBillNo());
            return transResult;
        }

        //参数校验
        if (StringUtils.isBlank(request.getMerchantBillNo())){
            subjectOutBills.setStatus(2);
            subjectOutBillsService.updateById(subjectOutBills);

            transResult.setTransStatus(2);
            transResult.setMsg(ErrorMsg.EMPTY_MERCHANTBILLNO);
            return transResult;
        }
        if (StringUtils.isBlank(request.getBillNo())){
            subjectOutBills.setStatus(2);
            subjectOutBillsService.updateById(subjectOutBills);

            transResult.setTransStatus(2);
            transResult.setMsg(ErrorMsg.EMPTY_BILLNO);
            return transResult;
        }
        if (null == request.getChannelCode()){
            subjectOutBills.setStatus(2);
            subjectOutBillsService.updateById(subjectOutBills);

            transResult.setTransStatus(2);
            transResult.setMsg(ErrorMsg.EMPTY_CHANNELCODE);
            return transResult;
        }
        if (StringUtils.isBlank(request.getSubjectCode())){
            subjectOutBills.setStatus(2);
            subjectOutBillsService.updateById(subjectOutBills);

            transResult.setTransStatus(2);
            transResult.setMsg(ErrorMsg.EMPTY_SUBJECTCODE);
            return transResult;
        }

        //获得bankCode
        CardBin cardBin = cardBinService.getBankInfoByCardNo(request.getCardNo());
        if (null == cardBin){
            subjectOutBills.setStatus(2);
            subjectOutBillsService.updateById(subjectOutBills);

            transResult.setTransStatus(2);
            transResult.setMsg(ErrorMsg.UNKNOWN_CARD_NO);
            return transResult;
        }

        request.setCardBin(cardBin.getBankCode());


        /**
         * 此处一定要注意，调用第三方渠道接口必须返回TransResult，不可为空，且保证异常在接口中已处理
         */
        switch (request.getChannelCode()){
            case CH0000001:
                try{
                    //TODO 调用第三方通道接口：快捷通
                    transResult=kjtpayService.df(request);
                }catch (Exception e){
                    log.error("CH0000001第三方通道快捷通交易接口调用失败"+request.getBillNo(),e);
                }
                break;
            case CH0000002:
                try{
                    //TODO 调用第三方通道接口：合利宝
                    transResult=helipayService.df(request);
                }catch (Exception e){
                    log.error("CH0000002第三方通道交易接口调用失败"+request.getBillNo(),e);
                }
                break;
            case CH0000003:
                try{
                    //TODO 调用第三方通道接口：畅捷支付
                    transResult=changpayService.df(request);
                }catch (Exception e){
                    log.error("CH0000003第三方通道交易接口调用失败"+request.getBillNo(),e);
                }
                break;
            case CH0000004:
                try{
                    //TODO 调用第三方通道接口：合利宝支付D1
                    transResult=helipayService.df(request);
                }catch (Exception e){
                    log.error("CH0000004第三方通道交易接口调用失败"+request.getBillNo(),e);
                }
                break;
            case CH0000005:
                try{
                    //TODO 调用第三方通道接口：畅捷支付D1
                    transResult=changpayService.df(request);
                }catch (Exception e){
                    log.error("CH0000005第三方通道交易接口调用失败"+request.getBillNo(),e);
                }
                break;
            case EMPTY0000001:
                //TODO 模拟渠道
                transResult = emptyService.df(request);
                break;
            default:
                transResult.setTransStatus(0);
                transResult.setMsg("未找到相应channelCode!");
        }

        //是否更新bills表状态
        boolean ifUpdateBillStatus = null != transResult && null != transResult.getTransStatus() && (transResult.getTransStatus().intValue()==1 || transResult.getTransStatus().intValue()==2);
        if (ifUpdateBillStatus){
            subjectOutBills.setStatus(transResult.getTransStatus());
            subjectOutBillsService.updateById(subjectOutBills);
        }

        return transResult;
    }
}
